package com.dimples.dd.xxl.job.plus.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * context工具
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/10/18
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    /**
     * Spring容器上下文
     */
    private static final Map<String, ApplicationContext> CONTEXT_HOLDER = new HashMap<>(1);

    /**
     * 获取Spring容器上下文
     *
     * @return 上下文
     */
    public static ApplicationContext getApplicationContext() {
        return CONTEXT_HOLDER.get(ApplicationContext.class.getName());
    }

    /**
     * 注入Spring容器上下文
     *
     * @param applicationContext 上下文
     * @throws BeansException bean异常
     */
    @Override
    public void setApplicationContext(@SuppressWarnings("NullableProblems") ApplicationContext applicationContext) throws BeansException {
        CONTEXT_HOLDER.put(ApplicationContext.class.getName(), applicationContext);
    }
}
